#include"Stock.h"
Stock::Stock()
{
	company = "no name";
	shares = 0;
	shares_val = 0;
	set_tot();
}

Stock::Stock(const string& name, int num, double val)
{
	company = name;
	shares = num;
	if (num < 0)
	{
		cout << "shares can't be lower than 0,auto set to 0\n";
		shares = 0;
	}
	shares_val = val;
	set_tot();
}

void Stock::buy(int num, double val)
{
	shares += num;
	shares_val = val;
	set_tot();
}

void Stock::sell(int num, double val)
{
	if (num < 0)
	{
		cout << "sell num can't be lower than 0,abort sale";
	}
	else if (num > shares)
	{
		cout << "sell num can't be higher than shares,abort sale";
	}
	else
	{
		shares -= num;
		shares_val = val;
		set_tot();
	}
}

void Stock::update(int val)
{
	shares_val = val;
	set_tot();
}

void Stock::show()
{
	ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
	streamsize prec = cout.precision(3);
	
	cout << "company:" << company << endl;
	cout << "shares:" << shares << endl;
	cout << "shares_val:" << shares_val << endl;
	cout << "tot_val:" << tot_val << endl;
	
	cout.setf(orig, ios_base::floatfield);
	cout.precision(prec);
}

double Stock::gettot() const
{
	return tot_val;
}

const Stock& Stock::topval(const Stock& s) const
{
	if (tot_val < s.gettot())
		return s;
	else
		return *this;
}
